{# SPDX-FileCopyrightText: © 2024 Abhishek Kumar
   Author: Abhishek Kumar <akumar17871@gmail.com>

   SPDX-License-Identifier: FSFAP
#}
{% set fileIds = concludedLicenseFileIds | merge(declaredLicenseFileIds) %}
{
  "@context": "https://spdx.org/rdf/3.0.0/spdx-context.jsonld",
  "@graph": [
    {
      "type": "NamespaceMap",
      "namespace": "{{ uri }}",
      "prefix": "URI"
    },
    {
      "@id": "creationInfo:creationInfo1",
      "type": "CreationInfo",
      "specVersion": "3.0.0",
      "created": "{{ 'now'|date('Y-m-d\\TH:i:s\\Z', 'UTC') }}",
      "createdBy": [
        "Person:SPDXRef-Actor-{{ userName | replace({' (': '-', ')': ''}) }}"
      ],
      "createdUsing": [
        "Tool:SPDXRef-Actor-{{ toolVersion }}"
      ],
      "comment": "<text>This document was created using license information and a generator from Fossology.</text>"
    },
    {
      "type": "SpdxDocument",
      "spdxId": "SpdxDocument:SpdxRef-DOCUMENT",
      "creationInfo": "creationInfo:creationInfo1",
      "name": "{{ documentName|e }}",
      "profileConformance": [
        "core",
        "software",
        "simpleLicensing",
        "expandedLicensing"
      ],
      "element": [
        "Tool:SPDXRef-Actor-{{ toolVersion }}",
        "Person:SPDXRef-Actor-{{ userName | replace({' (': '-', ')': ''}) }}",{{"\n"}}{% for packageId in packageIds %}
        "Package:SPDXRef-upload{{ packageId }}",{{"\n"}}{% endfor %}{% for fileId in fileIds %}
        "File:SPDXRef-item{{ fileId }}"{% if not loop.last %},{% endif %}{{"\n"}}{% endfor %}
      ],
      "rootElement": [{{"\n"}}{% for packageId in packageIds %}
        "Package:SPDXRef-upload{{ packageId }}"{% if not loop.last %},{% endif %}{{"\n"}}{% endfor %}
      ],
      "dataLicense": {
        "type": "simplelicensing_AnyLicenseInfo",
        "creationInfo": "creationInfo:creationInfo1",
        "description": "{{ licenseList[dataLicense].licenseObj.spdxId|replace({' ': '-'}) }}"
      }
    },{{"\n"}}{% if toolVersion is not empty %}
    {
      "type": "Tool",
      "spdxId": "Tool:SPDXRef-Actor-{{ toolVersion }}",
      "creationInfo": "creationInfo:creationInfo1",
      "name": "{{ toolVersion }}"
    },{% endif %}{{"\n"}}{% if userName is not empty %}
    {
      "type": "Person",
      "spdxId": "Person:SPDXRef-Actor-{{ userName | replace({' (': '-', ')': ''}) }}",
      "creationInfo": "creationInfo:creationInfo1",
      "name": "{{ userName | split(' ')[0] }}",
      "externalIdentifier": [
        {
          "type": "ExternalIdentifier",
          "externalIdentifierType": "email",
          "identifier": "{{ userName | split(' (')[1] | split(')')[0] }}"
        }   
      ]
    },{% endif %}{{"\n"}}{% if organization is not empty %}
    {
      "type": "Organization",
      "spdxId": "Organization:SPDXRef-Actor-{{ organization }}",
      "creationInfo": "creationInfo:creationInfo1",
      "name": "{{ userName | split(' ')[0] }}"
    },{% endif %}{% for licenseData in licenseList %}{% if licenseData.licenseObj.spdxId starts with 'LicenseRef-' or licenseData.licenseObj.shortName starts with 'LicenseRef-' %}
    {
      "type": "expandedlicensing_CustomLicense",
      "spdxId": "URI:{{ licenseData.licenseObj.spdxId|replace({' ': '-'})|e }}",
      "creationInfo": "creationInfo:creationInfo1",
      "name": "{{ licenseData.licenseObj.fullName|e }}",{{"\n"}}{% if licenseData.licenseObj.url is not empty %}
      "expandedlicensing_seeAlso": [
        "{{ licenseData.licenseObj.url }}"
      ],{{"\n"}}{% endif %}
      "simplelicensing_licenseText": "<text>{{ licenseData.licenseObj.text|replace({'<text>':'&lt;text&gt;', '</text>':'&lt;/text&gt;'})|replace({'\f':'','"':"'", '\n':''}) }}</text>"
    },{{"\n"}}{% endif %}{% endfor %}
    {{ packageNodes }}{% if concludedLicenseFileIds is not empty %}{% for packageId in packageIds %}
    {
      "type": "Relationship",
      "spdxId": "URI:SPDXRef-Relationship-{{ concludedLicenseFileIds | length }}",
      "from": "Package:SPDXRef-upload{{ packageId }}",
      "to": [{{"\n"}}{% for concludedLicenseFileId in concludedLicenseFileIds %}
        "File:SPDXRef-item{{ concludedLicenseFileId }}"{% if not loop.last %},{% endif %}{{"\n"}}{% endfor %}
      ],
      "relationshipType": "hasConcludedLicense",
      "creationInfo": "creationInfo:creationInfo1"
    }{% if not loop.last %},{{"\n"}}{% else %}{% if declaredLicenseFileIds is not empty %},{% endif %}{% endif %}{% endfor %}{{"\n"}}{% endif %}{% if declaredLicenseFileIds is not empty %}{% for packageId in packageIds %}
    {
      "type": "Relationship",
      "spdxId": "URI:SPDXRef-Relationship-{{ declaredLicenseFileIds | length }}",
      "from": "Package:SPDXRef-upload{{ packageId }}",
      "to": [{{"\n"}}{% for declaredLicenseFileId in declaredLicenseFileIds %}
        "File:SPDXRef-item{{ declaredLicenseFileId }}"{% if not loop.last %},{% endif %}{{"\n"}}{% endfor %}
      ],
      "relationshipType": "hasDeclaredLicense",
      "creationInfo": "creationInfo:creationInfo1"
    }{% if not loop.last %},{% endif %}{{"\n"}}{% endfor %}{% endif %}
  ]
}
